//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    showContent1:false,
    showContent2:true,
    items:[],
    isRead:false,
    isFeedBack:false,
    textareaV:'',
    feekbackStatus:0,
    feekbackStatusShow:true,
    str:'niasdfa,sdfasdf',
    pics: []//图片
  },
  onLoad(options){
    this.setData(options)
    this.getFeedBackDetail(options.recordId)
  },
  inputTextarea(e) {
    this.setData({
      textareaV: e.detail.value
    })
  },
  getFeedBackDetail(id){
    id=id||1
    wx.showLoading({
      title: '加载中',
    })
    app._post_form('/blade-api/feedBackDetail', {id}, (res) => {
      res.data.courseDate=res.data.courseDate.substr(0,10)
      this.setData(res.data)
      wx.hideLoading()
    })
  },
  showHandle(e){
    let cur=e.currentTarget.dataset.cur
      let labelText='showContent'+cur
      this.setData({
        [labelText]:!this.data[labelText]
      })
  },
  radioHandle(e){
    let feekbackStatus=e.currentTarget.dataset.cur
    this.setData({feekbackStatus})
  },
  // 是否阅读
  isReadHandle(){
    this.setData({isRead:!this.data.isRead})
  },
  addFeedbackHandel(){
    this.setData({
      isFeedBack:true
    })
  },
  //上传图片开始
  chooseImg (e) {
    let that = this, pics = this.data.pics;
    if (pics.length < 6) {
      wx.chooseImage({
        count: 6, // 最多可以选择的图片张数，默认9
        sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
        sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          wx.showToast({
            title: '正在上传...',
            icon: 'loading',
            mask: true,
            duration: 10000
          });
          wx.uploadFile({
            url: app.api_root+'blade-api/uploadFile', //接口地址
            filePath: tempFilePaths[0],
            name: 'file',
            header:{
              accessToken:   wx.getStorageSync('sign')
            },
            success (res){
              let data=JSON.parse(res.data)
              app.showMsg('上传成功')
              pics.push({pic:tempFilePaths[0],link:data.data.link});
              that.setData({
                pics: pics
              })
              wx.hideToast()
            }
          })
        },
      });
    } else {
      wx.showToast({
        title: '最多上传6张图片',
        icon: 'none',
        duration: 3000
      });

    }
  },
  // 删除图片
  deleteImg(e) {
    var pics = this.data.pics;
    var index = e.currentTarget.dataset.index;
    pics.splice(index, 1);
    this.setData({
      pics: pics,
    })
  },
  // 预览图片
  previewImg(e) {
    //获取当前图片的下标
    let index = e.currentTarget.dataset.index;
    var dataUrls = e.currentTarget.dataset.urls;
    let pics=[]
    if(dataUrls){
     pics = dataUrls.split(',').filter(item => item!=='')
    }else{
      //所有图片
      pics = this.data.pics.map((d)=>{
        return d.pic
      });
    }
    wx.previewImage({
      //当前显示图片
      current: pics[index],
      //所有图片
      urls: pics
    })
  },
  radioChange(e) {
    const items = this.data.items
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value === e.detail.value
    }
    this.setData({
      items
    })
  },
  bindFormSubmit(){
    if (this.data.textareaV == "") {
      app.showMsg("反馈内容不能为空！");
      return;
    }
    if(!this.data.isRead){
      app.showMsg("请先阅读课堂记录！");
      return;
    }
    if(this.data.feekbackStatus==0){
      app.showMsg("请选择完成情况！");
      return;
    }
    app.alert({
      title:'提示',
      content:'提交之后不支持修改， 确定提交吗？',
      showCancel:true,
      cb_confirm:()=>{
        let content=this.data.textareaV
        let id=this.data.recordId||1
        let recordUrl=''
        let feedBackStatus=this.data.feekbackStatus
        this.data.pics.forEach((d,i)=>{recordUrl+=d.link+','})
        app._post_form('/blade-api/addFeedBack', {content,id,recordUrl,feedBackStatus}, (res) => {
          app.showMsg(res.msg,()=>{
            this.getFeedBackDetail(this.data.recordId)
            this.setData({
              content:'',
              textareaV:'',
              pics:[]
            })
          });
        })
      },
      cb_cancel:()=>{console.log('取消')}
    })

  }
})
